package com.hyt.it.ogt.kq.service.gov.service.impl;

import java.util.ArrayList;
import java.util.List;

import javax.annotation.Resource;

import org.springframework.beans.BeanUtils;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.hyt.core.base.BaseServiceImpl;
import com.hyt.core.util.UUIDUtils;
import com.hyt.it.ogt.kq.common.gov.utils.tree.Tree;
import com.hyt.it.ogt.kq.service.gov.mapper.ExamMonitorMapper;
import com.hyt.it.ogt.kq.service.gov.model.entity.ExamMonitor;
import com.hyt.it.ogt.kq.service.gov.model.param.ExamMonitorParam;
import com.hyt.it.ogt.kq.service.gov.model.param.GetRoomTreeParam;
import com.hyt.it.ogt.kq.service.gov.model.vo.ExamMonitorVO;
import com.hyt.it.ogt.kq.service.gov.service.IExamMonitorService;

import lombok.extern.slf4j.Slf4j;

/**
 * <p>
 * 考试过程监控表 服务实现类
 * </p>
 *
 * @author liuq
 * @since 2021-04-22
 */
@Slf4j
@Service
public class ExamMonitorServiceImpl extends BaseServiceImpl<ExamMonitorMapper, ExamMonitor> implements IExamMonitorService {

	@Resource
	private ExamMonitorMapper examMonitorMapper;
	
	@Override
	public List<Tree> getRoomTree(GetRoomTreeParam param) {
		List<Tree> treeList = examMonitorMapper.getRoomTree(param);
		return treeList;
	}

	@Override
	@Cacheable(cacheNames = "monitor_page")
	public Page<ExamMonitorVO> page(Page<ExamMonitorVO> pageParam, ExamMonitorParam param) {
		Page<ExamMonitorVO> page = new Page<>();
		String area = "area";
		String place = "place";
		String room = "room";
		if (area.equals(param.getShowType())) {
			page = examMonitorMapper.selectAreaMonitorPage(pageParam, param);
		}
		if (place.equals(param.getShowType())) {
			page = examMonitorMapper.selectPlaceMonitorPage(pageParam, param);
		}
		if (room.equals(param.getShowType())) {
			page = examMonitorMapper.selectRoomMonitorPage(pageParam, param);
		}
		return page;
	}

	@Override
	public void initExamMonitor(String taskId, String timeId) {

		//清除无效的场次密码
		baseMapper.deleteExamMonitor(taskId,timeId);

		//查询需要生成密码的场次数据
		List<ExamMonitor> needExamMonitorList = baseMapper.selectNeedExamMonitor(taskId, timeId);

		if (CollectionUtils.isEmpty(needExamMonitorList)) {
			return;
		}

		List<ExamMonitor> examMonitors = new ArrayList<>();
		needExamMonitorList.stream().forEach(examMonitor -> {
			ExamMonitor monitor = new ExamMonitor();
			BeanUtils.copyProperties(examMonitor,monitor);
			monitor.setExamEnded(false)
					.setDownloadPaper(false)
					.setExamStarted(false)
					.setDownloadRoomData(false)
					.setDownloadPaper(false)
					.setDownloadDecode(false)
					.setUploadResult(false)
					.setTaskId(taskId)
					.setId(UUIDUtils.newSortUUID());
			examMonitors.add(monitor);
		});

		this.saveBatch(examMonitors);

	}

	@Override
	public List<ExamMonitor> getBatchByTimeRoomCode(String taskCode, String roomCode, String timeCodes) {
		return examMonitorMapper.getBatchByTimeRoomCode(taskCode, roomCode, timeCodes);
	}

	@Override
	public ExamMonitor getByTimeRoomCode(String taskCode, String roomCode, String timeRoomCode) {
		return examMonitorMapper.getByTimeRoomCode(taskCode, roomCode, timeRoomCode);
	}

	@Override
	public ExamMonitor getByTimeAndRoomCode(String taskCode, String roomCode, String timeCode) {
		return examMonitorMapper.getByTimeAndRoomCode(taskCode, roomCode, timeCode);
	}

}
